package com.leetcode.LC;

import java.util.Arrays;

public class LC1356 {
    public int[] sortByBits(int[] arr) {

        double[] doubles = new double[arr.length];
        for (int i = 0; i < arr.length; i++) {
            doubles[i]=toBin(arr[i]);
        }
        Arrays.sort(doubles);

        for (int i = 0; i < arr.length; i++) {
            arr[i]= (int) (doubles[i]%100000);
        }
        return arr;
    }

    public double toBin(int a){
        String s = Integer.toBinaryString(a);
        int count=0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i)=='1'){
                count++;
            }
        }

        return count*100000+a;
    }

}
